package com.yunda.nebula.gateway.wrapper;

import com.yunda.nebula.gateway.core.entity.FetchAppInfo;
import com.yunda.nebula.gateway.core.entity.Result;
import org.springframework.http.HttpStatus;
import org.springframework.web.context.request.async.DeferredResult;



public class DeferredResultWrapper {
  private static final Result<FetchAppInfo>
      NOT_MODIFIED_RESPONSE_RESULT = Result.failed(HttpStatus.NOT_MODIFIED.value(),"没有修改");

  private DeferredResult<Result<FetchAppInfo>> result;


  public DeferredResultWrapper(long timeoutInMilli) {
    result = new DeferredResult<>(timeoutInMilli, NOT_MODIFIED_RESPONSE_RESULT);
  }


  public void onTimeout(Runnable timeoutCallback) {
    result.onTimeout(timeoutCallback);
  }

  public void onCompletion(Runnable completionCallback) {
    result.onCompletion(completionCallback);
  }

  /**
   * The namespace name is used as a key in client side, so we have to return the original one instead of the correct one
   */
  public void setResult(FetchAppInfo fetchAppInfo) {
    Result data = Result.data(fetchAppInfo);
    result.setResult(data);
  }

  public DeferredResult<Result<FetchAppInfo>> getResult() {
    return result;
  }
}
